package com.itheima.servlet.myservlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

public class ContextDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = getServletContext();
        String globleEncodingValue = context.getInitParameter("globleEncoding");
        System.out.println(globleEncodingValue);
        Enumeration<String> it = context.getInitParameterNames();
        while (it.hasMoreElements()) {
            String s = it.nextElement();
            String sValue = context.getInitParameter(s);
            System.out.println(s + "=" + sValue);
        }
        //获取项目的虚拟路径
        String contextPath = context.getContextPath();
        System.out.println(contextPath);
        //获取绝对路径
        //注意这里获取的路径是编译后的路径
        String realPath = context.getRealPath("/");
        System.out.println(realPath);
        String bRealPath = context.getRealPath("/b.txt");
        System.out.println(bRealPath);
        String aRealPath = context.getRealPath("/WEB-INF/a.txt");
        System.out.println(aRealPath);
        //在src目录下的文件比较特殊需要加class
        String cRealPath = context.getRealPath("/WEB-INF/class/c.txt");
        System.out.println(cRealPath);
        //设置共享数据key必须是String但是值可以是任意类型
        //注意共享域里的数据都是以键值对的形式存储的
        context.setAttribute("username", "zhangsan");
//移除共享数据
//context.removeAttribute("username");
    }


}
